package com.demo.controller;

import com.demo.bean.FixedEvent;
import com.demo.bean.User;
import com.demo.service.EventManageService;
import com.demo.service.RedisService;
import com.demo.service.ResultService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;

import static com.demo.Tools.DateCompute.getNowWeekNum;

/**
 * @author 李天航
 */
@RestController
@RequestMapping("/result")
public class ResultController {
    @Resource
    ResultService resultService;
    @Resource
    EventManageService eventManageService;
    @Resource
    RedisService redisService;

    private final String mustFillLock="mustFillLock";
    /**
     * 添加日程
     * @param result 日程信息
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/add")
    public Object addResult(FixedEvent result, HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        result.setUserAccount(user.getUserAccount());
        return resultService.addResult(result);
    }

    /**
     * 删除一项日程
     * @param eventId 日程id
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/delete")
    public Object deleteResult(Integer eventId, HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return resultService.deleteResult(eventId,user.getUserAccount());
    }

    /**
     * 查看所有日程
     * @param weekNum 周数
     * @param session 缓存
     * @return status,msg,results
     */
    @PostMapping("/getall")
    public Object getAllResults(Integer weekNum,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return resultService.getResults(new FixedEvent(user.getUserAccount(), weekNum));
    }

    /**
     * 修改日程
     * @param fixedEvent 日程编号和修改的
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/update")
    public Object updateResult(FixedEvent fixedEvent,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        fixedEvent.setUserAccount(user.getUserAccount());
        return resultService.updateResult(fixedEvent);
    }

    /**
     * 打卡一项日程
     * @param eventId 日程编号
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/finish")
    public Object finishResult(Integer eventId, HttpSession session)
    {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            User user = (User)  redisService.getSessionData(session.getId(),"user");
            return resultService.finishResult(eventId, user.getUserAccount());
        }catch (Exception e)
        {
            resultMap.put("status",500);
            resultMap.put("msg","打卡失败");
            e.printStackTrace();
        }
        return resultMap;
    }
    @PostMapping("/unfinish")
    public Object unfinishResult(Integer eventId,HttpSession session)
    {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            User user = (User)  redisService.getSessionData(session.getId(),"user");
            return resultService.unfinishResult(eventId, user.getUserAccount());
        }catch (Exception e)
        {
            resultMap.put("status",500);
            resultMap.put("msg","打卡失败");
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 活动装填
     * @param session 缓存
     * @param weekNum 周数
     * @return status,msg
     */
    @PostMapping("/fill")
    public Object fillFixedEvent(HttpSession session,Long weekNum)
    {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        if(weekNum<getNowWeekNum())
        {
            resultMap.put("status",500);
            resultMap.put("msg","不安排过去的活动哦");
            return resultMap;
        }
        session.setAttribute(mustFillLock+weekNum,"unlock");
        try {
            User user = (User)  redisService.getSessionData(session.getId(),"user");
            return eventManageService.fillFixedEvents(user.getUserAccount(), weekNum);
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    /**
     * 在用户修改的基础上对日程表进行修改
     * @param session 缓存
     * @param weekNum 周数
     * @return status,msg
     */
    @PostMapping("/manage")
    public Object manageAfterUpdates(HttpSession session,Long weekNum,String algorithmName)
    {
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        if(weekNum<getNowWeekNum())
        {
            resultMap.put("status",500);
            resultMap.put("msg","不安排过去的活动哦");
            return resultMap;
        }
        try {
            if(session.getAttribute(mustFillLock+weekNum)==null)
            {
                resultMap.put("status",500);
                resultMap.put("msg","请先装填固定活动,再进行任务的安排");
                return resultMap;
            }
            session.setAttribute(mustFillLock+weekNum,null);
            User user = (User)  redisService.getSessionData(session.getId(),"user");
            if(algorithmName==null||algorithmName.equals("")){
                algorithmName="loadBalanceFirst";
            }
            return eventManageService.fillTasks(user.getUserAccount(), weekNum,algorithmName );
        }catch (Throwable e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }
    @PostMapping("/max/interval")
    public Object getMaxIntervalTime(HttpSession session, int weekNum)
    {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            User user = (User)  redisService.getSessionData(session.getId(),"user");
            Long maxIntervalTime = resultService.maxTaskTime(new FixedEvent(user.getUserAccount(), weekNum));
            if(maxIntervalTime!=-1) {
                resultMap.put("status", 200);
                resultMap.put("msg", "查询成功");
                resultMap.put("maxIntervalTime", maxIntervalTime);
            }else {
                resultMap.put("status", 200);
                resultMap.put("msg", "日常表中还没有活动");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg","查询失败");
        }
        return resultMap;
    }
}
